<template>
	<vCommonPage class="dybg" type="1" :title="$route.query.title" v-loading="loading" element-loading-text="正在打印中"
		element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, 0.8)">
		<div slot="customContent" class="contentX zyqd">
			<div class="flex  spaceBt">
				<el-date-picker v-model="dateValue" type="daterange" size="large" range-separator="至"
					start-placeholder="开始日期" end-placeholder="结束日期" @change="dataChange"
					:picker-options="pickerOptions">
				</el-date-picker>
				<div class="title">
					<span
						v-if="$store.state.user.userinfo.patientName">患者：{{$store.state.user.userinfo.patientName}}</span>
				</div>
			</div>r
			<div class="tableBox">
				<el-table class="table" :header-cell-style="$tableTitleStye" :cell-style="{color:'#435C7A'}"
					:data="tableData" :show-overflow-tooltip="true">
					<el-table-column type="index" label="序号" width="100"></el-table-column>
					<el-table-column prop="reportId" label="项目编号"></el-table-column>
					<el-table-column prop="reportName" label="报告名称"></el-table-column>
					<!-- <el-table-column prop="proState" label="报告状态"></el-table-column> -->
					<!-- 0 未出   1 已出  2 已打印 -->
					<el-table-column label="报告状态">
						<template slot-scope="scope">
							<p v-if="scope.row.status ==0" class="hui">未出</p>
							<p v-if="scope.row.status ==1" class="green">已出</p>
							<p v-if="scope.row.status ==2" class="hui">已打印</p>
							</el-button>
						</template>
					</el-table-column>
					<el-table-column label="操作">
						<template slot-scope="scope">
							<el-button :class="scope.row.status  ==  1 ?'elBtn':'elBtn ocatipal'" @click="dy(scope.row)"
								type="success" size="20">打印
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="bottomBox" style="justify-content: space-between;">
				<div>
					<el-button type="primary" class="BtnTextHome" @click="goBack()">返回</el-button>
					<el-button type="primary" class="BtnTextHome" @click="goBackHome()">首页</el-button>
				</div>
				<el-button type="success" class="BtnTextGreen"
					style="width: 180px !important;margin-left: 525px !important;" @click="getPrintReportlAll()">全部打印
				</el-button>
			</div>
		</div>
	</vCommonPage>
</template>
<script>
	import {
		getExamList,
		getExamDetail,
		getTestList,
		getTestDetail,
		addReportPrintLog
	} from "@api/apiBox/dybg"
	import {
		SimplerPrinter
	} from '@/api/tools/Hardware';
	import {
		MessageBox,
		Loading
	} from "element-ui";
	import moment from 'moment'
	export default {
		name: "zyqd",
		data() {
			return {
				tableData: [],
				simplePrinter: new SimplerPrinter(),
				loading: false,
				printList: [],
				printMuchState: false,
				dateValue: '',
				beginDate: moment(new Date).format('yyyy-MM-DD'),
				endDate: moment(new Date).format('yyyy-MM-DD'),
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				}
			};
		},
		methods: {
			//获取全部报告详情 点击全部打印
			async getPrintReportlAll() {
				this.ids = [];
				//遍历所有报告进行打印
				let index = 0;
				this.loading = true;
				let canPrint = false;
				for (let item of this.tableData) {
					if (item.status == 1) {
						canPrint = true;
						await this.timeAdd();
						await this.printReport(item, 1);
					}
				}
				this.loading = false;
				if (canPrint) {
					await this.getAllReport();
					MessageBox.alert("打印成功，请从下方取走报告！", {
						confirmButtonText: "确定",
						callback: action => {
							MessageBox.close();
							setTimeout(() => {
								this.$router.push("/");
							}, 500);
						}
					});
				} else {
					MessageBox.alert('没有可以打印的报告！', {
						confirmButtonText: "确定",
						callback: action => {
							MessageBox.close();
						}
					});
				}
			},
			async timeAdd() {
				return new Promise((resolve, reject) => {
					//使用定时器进行延时发送通知
					setTimeout(() => {
						resolve();
					}, 1000)
				})
			},
			async getDetail(item) {
				if (this.$route.query.code == 'test') {
					//获取打印报告html地址
					let data = {
						data: {
							reportId: item.reportId,
							patientId: this.$store.state.user.userinfo.patientId,
						},
						that: this
					}
					const res = await getTestDetail(data);
					return res.data;
				}

				if (this.$route.query.code == 'exam') {
					//获取打印报告html地址
					let data = {
						data: {
							reportId: item.reportId,
							beginDate: this.beginDate,
							patientId: this.$store.state.user.userinfo.patientId,
						},
						that: this
					}
					const res = await getExamDetail(data);
					return res.data;
				}

			},
			//打印报告
			async printReport(item, type) {
				let newRes = await this.getDetail(item);
				//1:报告打印机,2:凭条打印机,3:身份证读卡器
				let name = this.getPrintNameByPort(1);
				let printUrl = newRes.reportUrl;
				if (printUrl) {
					printUrl = 'http://192.168.0.15:80' + printUrl;
					// printUrl = 'http://220.163.44.58:30001/report-files/test/352021111544.pdf';
					if (type == 0) {
						this.loading = true;
					}
					try {
						const res = await this.simplePrinter.PrintFromHttp(printUrl, name);
						if (type == 0) {
							this.loading = false;
						}
						this.sendPrintRes(item);
						if (type == 0) {
							await this.getAllReport();
							MessageBox.alert("打印成功，请从下方取走报告！", {
								confirmButtonText: "确定",
								callback: action => {
									MessageBox.close();
									setTimeout(() => {
										this.$router.push("/");
									}, 500);
								}
							});
						}
					} catch (e) {
						this.loading = false;
						MessageBox.alert('打印' + item.reportName + '报告出错：', e, {
							confirmButtonText: "确定",
							callback: action => {
								MessageBox.close();
								setTimeout(() => {
									this.$router.push("/");
								}, 500);
							}
						});
					}
				} else {
					MessageBox.alert('获取不到打印报告:' + item.reportName + '的内容!' + e, {
						confirmButtonText: "确定",
						callback: action => {
							MessageBox.close();
						}
					});
				}
			},
			getPrintNameByPort(devtype) {
				let term = this.$store.state.terminfo.deviceConfs;
				console.log(term, 'name')
				for (let item of term) {
					console.log(item)
					if (item.deviceType == devtype) {
						return item.deviceModel;
					}
				}
				return "";
			},
			async sendPrintRes(item) {
				let data = {
					data: {
						patientId: this.$store.state.user.userinfo.patientId,
						patientName: this.$store.state.user.userinfo.patientName,
						reportId: item.reportId,
						reportName: item.reportName
					},
					that: this
				}
				await addReportPrintLog(data);
			},
			//获取打印报告详情 点击报告打印
			async dy(item) {
				console.log(item)
				//报告进行打印
				if (item.status == 1) {
					//打印单个报告
					await this.printReport(item, 0);

				}
				if (item.status == 0) {
					MessageBox.alert('报告:' + item.reportName + '状态为未出,请选择打印状态已出的报告!', {
						confirmButtonText: "确定",
						callback: action => {
							MessageBox.close();
						}
					});
				}
			},
			//获取报告列表
			async getAllReport() {
				console.log(this.$store.state)
				if (this.$route.query.code == 'test') {
					let data = {
						data: {
							//1116247 test  1035832  exam
							// patientId: 70626,
							patientId: this.$store.state.user.userinfo.patientId,
							beginDate: this.beginDate
						},
						that: this
					}
					// getTestList getExamList
					const res = await getTestList(data);
					this.tableData = res.data;
					console.log(res, '获取报告列表')
				}

				if (this.$route.query.code == 'exam') {
					let data = {
						data: {
							// patientId: 70626,
							patientId: this.$store.state.user.userinfo.patientId,
							beginDate: this.beginDate,
							endDate: this.endDate
						},
						that: this
					}
					// getTestList getExamList
					const res = await getExamList(data);
					this.tableData = res.data;
					console.log(res, '获取报告列表')
				}

			},
			async dataChange(date) {
				this.beginDate = moment(date[0]).format('yyyy-MM-DD');
				this.endDate = moment(date[1]).format('yyyy-MM-DD');
				this.getAllReport();
			}
		},
		computed: {},
		components: {},
		mounted() {},
		created() {
			if (this.$route.query.code == 'test') {
				//初始化所有检验都改成查两天
				this.beginDate = this.$getTime.fun_date(-1);
			}
			this.getAllReport();
		}
	};
</script>
<style scoped>
	.flex {
		display: flex;
		padding: 0px 30px;
		align-items: center;
	}

	.red {
		color: #FE5F5F;
	}

	.hui {
		color: #A1ADBB;
	}

	.deepHui {
		color: #435C7A;
	}

	.spaceBt {
		justify-content: space-between;
		margin-top: 10px;
	}



	.greenBottom {
		height: 90px;
		background: #46C349;
		border-radius: 6px;
		display: flex;
		padding: 0px 15px;
		margin-left: 0px;
		margin-right: 20px;
		/*实现垂直居中*/
		align-items: center;
	}

	.upPage {
		margin-left: 520px;
	}

	.BtnText {
		text-align: center;
		width: 100%;
		font-size: 3.1vw;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
	}

	.totalPay {
		font-size: 3.1vw;
		font-family: Source Han Sans CN;
		font-weight: 400;
		margin-left: 300px;
		padding-top: 20px;
	}

	.contentX {
		width: 100%;
		height: 100%;

	}

	.title {
		font-size: 2.75rem;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #435C7A;
		text-align: right;
		margin-bottom: 0rem;
	}

	.elBtn {
		font-size: 1.75rem;
		padding: 15px;
		padding-top: 20px;
		background: #46C349;
		box-shadow: 0px 1px 27px 0px rgba(42, 103, 177, 0.09);
		border-radius: 8px;
	}

	.ocatipal {
		opacity: 0.5;
		border-radius: 8px;
	}

	.table {
		margin-top: 30px;
		width: 95%;
		font-size: 2.028571rem;
		font-weight: 400;
		margin-left: 2.6%;
	}

	@media (min-height: 0px) and (max-height: 1910px) {

		.tableBox {
			margin-top: 15px;
			max-height: 600px;
			overflow: auto;
			overflow-x: hidden;
			borderRadius: '8px 8px 0px 0px';
		}

	}

	@media (min-height: 1919px) {

		.tableBox {
			margin-top: 15px;
			max-height: 550px;
			overflow: auto;
			overflow-x: hidden;
			borderRadius: '8px 8px 0px 0px';
		}

	}



	.bottomBox {
		display: flex;
		margin-top: 10px;
	}



	.pageBox {
		margin-left: 10px;
		margin-right: 10px;
		display: flex;
		font-size: 3.5vw;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #435C7A;
		/*实现垂直居中*/
		align-items: center;
	}

	.BtnText {
		text-align: center;
		width: 100%;
		font-size: 3.1vw;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
	}
</style>
